package com.pan.games.play;

/**
 * @author by panstark
 * @description
 * @notice
 * @date 2021/3/4
 */
public class BuySellStock {


    /**
     * 只可以买一次，卖一次
     * @param prices
     * @return
     */
    public static int maxProfitOne(int[] prices) {

        int buyPrice;
        int sellPrice;
        int earnBest=0;
        for (int i = 0; i < prices.length; i++) {
            buyPrice = prices[i];
            for (int j = i+1; j < prices.length; j++) {
                sellPrice = prices[j];
                int earnToday=sellPrice-buyPrice;

                if(earnToday>earnBest){
                    earnBest=earnToday;
                }
            }
        }
        return earnBest;
    }

    /**
     * 可以买卖多次
     * @param prices
     * @return
     */
    public static int maxProfitMy(int[] prices,int l) {

        int maxProfit = 0;
        for (int i = l; i < prices.length; i++) {

            System.out.println("i:"+i+",l:"+l);
            int profit = maxProfitOneDay(prices,i);
            if(profit>maxProfit){
                maxProfit=profit;
            }
        }
        return maxProfit;
    }

    private static int maxProfitOneDay(int[] prices, int i) {

        int nowlength = prices.length-i;
        if(nowlength==1||nowlength==0){
            return 0;
        }
        if(nowlength==2){
            int profit = prices[prices.length-1]-prices[prices.length-2];
            return profit>0?profit:0;
        }

        int maxProfit = 0;
        for (int j = i+1; j < prices.length; j++) {
            if(prices[j]<=prices[i]){
                continue;
            }
            int profit = prices[j]-prices[i];
            int nextProfit =0;

            nextProfit = profit+maxProfitMy(prices,j+1);

            if(nextProfit>maxProfit){
                maxProfit=nextProfit;
            }
        }
        return maxProfit;
    }



    public static void main(String[] args) {
        int[] prices = new int[]{6,1,3,2,4,7};
        int maxprofit = maxProfitMy(prices,0);
        System.out.println("maxProfitOne:"+maxprofit);
    }
}
